define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'album/catalog/index' + location.search,
                    add_url: 'album/catalog/add',
                    edit_url: 'album/catalog/edit',
                    multi_url: 'album/catalog/multi',
                    import_url: 'album/catalog/import',
                    table: 'album_catalog',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'description', title: __('Description'), operate: false},
                        {field: 'imagecount', title: '图片数量', operate: false},
                        {field: 'user.nickname', title:'用户昵称',operate: false},
                        {field: 'user.mobile', title:'用户手机',operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, 
                        events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'detail',
                                title: __('图片预览'),
                                classname: 'btn btn-xs btn-primary btn-ajax',
                                icon: 'fa fa-file-image-o',
                                url: 'album/catalog/photo',
                                success: function (data, ret) {
                                    console.log(data, ret);
                                    Layer.photos({
                                        photos:{
                                            data: data,
                                            start:0,
                                        },
                                        closeBtn:1,
                                    });
                                    return false;
                                },
                                error: function (data, ret) {
                                    console.log(data, ret);
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
